Launching an Android application from within unity
Manual     Reference     Scripting   
Unity Manual > Getting Started with Android Development > Integrating Unity with Eclipse > Launching an Android application from within unity

Launching an Android application from within unity

If you have followed the guide on integrating unity with eclipse then you will have a basis for doing this. This example will show you how to launch your media gallery from your unity android application.

Adding the launch control to Unity

What you need to do is add some control into unity that will call into the Java project and launch the unity application. In this example a button will be used.

Adding the button

In your unity project create new script. Within the script add an OnGUi function. In this function we need to add the button and makes some calls to Java. The calls to Java are made using the AndroidJavaClass and AndroidJavaObject. What you need to do is get the currentActivity and then call a function in that to start an intent.

	void OnGUI()
	{
		if(GUI.Button(new Rect(100,100,100,100) , "launch"))
		{
			AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
			AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
			jo.Call("Launch");
		}
	}

Now that is in place you need to build your project and move the contents of the assets folder in project directory over to the eclipse project.

The Java Code

Previously you had written the button to launch the activity on Android through an intent. You now need to write the Java Code that will actually do this.

private Intent myIntent;
        myIntent = new Intent();

        myIntent.setAction(Intent.ACTION_VIEW);
        myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    public void Launch()
    {    	
    	startActivity(myIntent);
    }

If you now build and run your application from Eclipse you should now have a Unity application that runs on your Android device with a button named launch that when pressed the media gallery on your device launches. You can then press the back button to go back to your Android device and repeat the process.

Page last updated: 2011-02-21